package br.org.groupware_workbench.collabElement.coop.recommendMgr.internal;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.org.groupware_workbench.collabElement.coop.recommendMgr.api.Recommend;
import br.org.groupware_workbench.coreutils.GenericDAO;
import br.org.groupware_workbench.coreutils.GenericEntity;

public class RecommendDAO extends GenericDAO<Recommend> {

    public RecommendDAO() {
        super(Recommend.class);
    }
  
    @SuppressWarnings("unchecked")
    public List<Recommend> listGenericEntityByNumRelated(long idInstance, GenericEntity genericEntity, int numRelated) {
        EntityManager em = getEntityManager();
        String queryString = "SELECT a FROM " + Recommend.class.getSimpleName() +
                            " a WHERE a.idInstance = :idInstance AND" +
                            " (a.genericEntityFrom = :genericEntity OR a.genericEntityTo = :genericEntity)" +
                            " ORDER BY a.similaritysProduct DESC";
        Query query = em.createQuery(queryString);
        query.setFirstResult(0);
        query.setMaxResults(numRelated);
        query.setParameter("genericEntity", genericEntity);
        query.setParameter("idInstance", idInstance);
        System.out.println("Numero de registros related : " + query.getResultList().size());
        return query.getResultList();
    }
}
